package com.xkdx.caipiao.my;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.xkdx.caipiao.LotteryHallActivity;
import com.xkdx.caipiao.MyLotterActivity;
import com.xkdx.caipiao.R;
import com.xkdx.caipiao.ThreadActivity;
import com.xkdx.caipiao.application.MyApplication;
import com.xkdx.caipiao.module.network.AbsModule;
import com.xkdx.caipiao.pay.PayActivity;
import com.xkdx.caipiao.presistence.autologin.AutoLoginAction;
import com.xkdx.caipiao.presistence.autologin.AutoLoginModule;
import com.xkdx.caipiao.presistence.autologin.AutoLoginPresistence;
import com.xkdx.caipiao.presistence.hongbao.HongbaoAction;
import com.xkdx.caipiao.presistence.hongbao.HongbaoInfo;
import com.xkdx.caipiao.presistence.hongbao.HongbaoModule;
import com.xkdx.caipiao.presistence.hongbao.HongbaoPresistence;
import com.xkdx.caipiao.presistence.login.Login;
import com.xkdx.caipiao.presistence.login.LoginInfo;
import com.xkdx.caipiao.presistence.quert.TypeQueryInfo;
import com.xkdx.caipiao.presistence.quert.TypeQueryItemInfo;
import com.xkdx.caipiao.presistence.trad.TradAction;
import com.xkdx.caipiao.presistence.trad.TradFromOrderAction;
import com.xkdx.caipiao.presistence.trad.TradFromOrderModule;
import com.xkdx.caipiao.presistence.trad.TradFromOrderPresistence;
import com.xkdx.caipiao.presistence.trad.TradModule;
import com.xkdx.caipiao.presistence.trad.TradPresistence;
import com.xkdx.caipiao.statics.IConstants;
import com.xkdx.caipiao.util.Md5Utils;
import com.xkdx.caipiao.util.SharePrefenceUtil;

public class OrderPayActivity extends ThreadActivity {
	TradAction action;
	TradModule module;
	TradPresistence presistence;
	boolean isDelOrder = false;
	boolean isVisiable = false;

	String issueid;
	String lotteryid;
	String betting;
	String beishu;
	String id;
	
	float money;
	float current;
	TextView tv_name, tv_pay, tv_order_price,select_red,tv_zhifu,hongbao;
	Button pay_sure;
	ImageView arrow;
	LinearLayout ll_select;
	ListView listView;
	List<HongbaoInfo> list2;

	AutoLoginAction loginAction;
	AutoLoginModule loginModule;
	AutoLoginPresistence loginPresistence;
	LoginInfo info;
	
	HongbaoAction action2;
	HongbaoModule module2;
	HongbaoPresistence presistence2;

	TradFromOrderAction fromOrderaction;
	TradFromOrderModule fromOrdermodule;
	TradFromOrderPresistence fromOrderpresistence;
	String issure, pid; // 期数和父id
	int clickpage;
	
	//1:确认支付。 0:请求红包列表
	int isPastRedpacket; 
	
	MyAdapter adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pay_caipiao);
		TypeQueryInfo typeQueryInfo = getLotteryType();
		if (!LotteryHallActivity.lotteryid.equals("")) {
			lotteryid = LotteryHallActivity.lotteryid;
		}
		select_red = (TextView) findViewById(R.id.select_hongbao);
		hongbao = (TextView) findViewById(R.id.select_hongbao1);
		ll_select = (LinearLayout) findViewById(R.id.see_item_number);
		ll_select.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				if (isVisiable) {
					hongbao.setText("");
					float mon = Float.valueOf(money);
					DecimalFormat df = new DecimalFormat("#0.00");
					String money = df.format(mon);
					tv_zhifu.setText(String.valueOf(money));
					isVisiable = false;
					arrow.setImageResource(R.drawable.down_arrow_more_caipiao);
					listView.setVisibility(View.GONE);
				} else {
					isVisiable = true;
					arrow.setImageResource(R.drawable.up_arrow_more_caipiao);
					listView.setVisibility(View.VISIBLE);
				}
				startThread2();
				
				
			}
			
		});

		
		
		tv_zhifu = (TextView) findViewById(R.id.tv_zhifu);
		
		
	//	tv_zhifu.setText(money);
		listView = (ListView) findViewById(R.id.lv_list);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				HongbaoInfo info = (HongbaoInfo) adapter.getItem(arg2);
				float mon1 = Float.valueOf(info.getFacevalue()) / 100;
				hongbao.setText(String.valueOf(mon1)+"元");
				//如果点击的这个是红的将其改变为白色的
				if(module2.list1.get(arg2).isChecked){
					module2.list1.get(arg2).isChecked = false;
					hongbao.setText("");
					float mon = Float.valueOf(money);
					DecimalFormat df = new DecimalFormat("#0.00");
					String money = df.format(mon);
					tv_zhifu.setText(String.valueOf(money));
					
				}else{//如果点击的是白的，先将所有变白
					for (HongbaoInfo hb : module2.list1) {
						hb.isChecked  = false;
					}
					module2.list1.get(arg2).isChecked = true;
					tv_zhifu.setText(String.valueOf(money-mon1));
				}
				adapter.notifyDataSetChanged();

				
				
				
				id = info.getId();
			}
		});
//		listView.setAdapter(new MyAdapter());
	
		
		
		arrow = (ImageView) findViewById(R.id.arrow);
		tv_name = (TextView) findViewById(R.id.tv_qici);
		tv_pay = (TextView) findViewById(R.id.tv_pay);
		pay_sure = (Button) findViewById(R.id.pay_sure);

		betting = getIntent().getStringExtra("betting");
		beishu = getIntent().getStringExtra("beishu");
		money = Integer.parseInt(getIntent().getStringExtra("money"));
		issure = getIntent().getStringExtra("issure");
		pid = getIntent().getStringExtra("pid");

		tv_order_price = (TextView) findViewById(R.id.order_price);
		float mon = Float.valueOf(money);
		DecimalFormat df = new DecimalFormat("#0.00");
		String money = df.format(mon);
		tv_order_price.setText(money);
		tv_zhifu.setText(money);
		String str = getLotteryNameFormLotteryID(lotteryid);

		List<TypeQueryItemInfo> list = typeQueryInfo.getList();
		for (TypeQueryItemInfo typeQueryItemInfo : list) {
			if (typeQueryItemInfo.getLotteryid().equals(lotteryid)) {
				issueid = typeQueryItemInfo.getIssueid();
			} else if (typeQueryItemInfo.getLotteryid().equals(lotteryid)) {
				issueid = typeQueryItemInfo.getIssueid();
			} else if (typeQueryItemInfo.getLotteryid().equals(lotteryid)) {
				issueid = typeQueryItemInfo.getIssueid();
			} else if (typeQueryItemInfo.getLotteryid().equals(lotteryid)) {
				issueid = typeQueryItemInfo.getIssueid();
			}

		}

		if (TextUtils.isEmpty(issure)) {
			tv_name.setText(str + issueid + " 期");
		} else {
			tv_name.setText(issure);
		}
		StartRequest(); // 获取当前余额
	}

	class MyAdapter extends BaseAdapter{

	
		private List<HongbaoInfo> list2;

		public MyAdapter(List<HongbaoInfo> list2) {
			this.list2 = list2;
		}
		public int getCount() {
			return list2.size();
		}

		@Override
		public Object getItem(int position) {
			return list2.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			Holder holder = null;
			if (convertView == null) {
				holder = new Holder();
				convertView = LayoutInflater.from(getApplicationContext())
						.inflate(R.layout.select_red_pake_caipiao, null);
				holder.tv_red = (TextView) convertView
						.findViewById(R.id.tv_red);
				holder.tv_ye = (TextView) convertView
						.findViewById(R.id.tv_ye);
				holder.cb = (ImageView) convertView.findViewById(R.id.iv);
				convertView.setTag(holder);
			} else {
				holder = (Holder) convertView.getTag();
			}
			float mon1 = Float.valueOf(list2.get(position).getFacevalue()) / 100;
			DecimalFormat df = new DecimalFormat("#0.00");
			String money = df.format(mon1);

			holder.tv_red.setText(list2.get(position).getDetail());

			holder.tv_ye.setText(money+"元");
			
			holder.cb.setImageResource(R.drawable.button_w_caipiao);
			if (list2.get(position).isChecked) {
				
				holder.cb.setImageResource(R.drawable.button_red_caipiao);
			}
			
			return convertView;
		}
		
	}
	/**
	 * 判断是否包含
	 * @param strs
	 * @param str
	 * @return
	 */
	public boolean isContanie(String[] strs,String str){
		for (int i = 0; i < strs.length; i++) {
			Log.i("===========",strs[i]);
			if(str.equals(strs[i])){
				return true;
			}
		}
		return false;
	}
	
	class Holder {
		TextView tv_red, tv_ye;
		ImageView red,cb;
		
	}

	
	
	/**
	 * 从订单详情页面进入
	 */
	private void StartRequestFromOrderDetail() {

	}

	/**
	 * 获取当前余额
	 */
	private void StartRequest() {
		if (!(dataLoadDialog.isShowing())) {
			dataLoadDialog.show();
			dataLoadDialog.setContentView(R.layout.progressbar_caipiao);
		}
		info = getLoginInfo();
		String md5 = info.getId()+info.getLogintoken()+IConstants.key;
		String signcode = Md5Utils.md5(md5);
		if (null != info) {
			loginAction = new AutoLoginAction(info.getId(),
					info.getLogintoken(),signcode);
			loginModule = new AutoLoginModule(OrderPayActivity.this);
			loginPresistence = new AutoLoginPresistence(this);
			loginPresistence.setActitons(loginAction);
			loginPresistence.setModule(loginModule);
			loginPresistence.execute();

		} else {

			Toast.makeText(this, "请先登录", Toast.LENGTH_SHORT).show();
			Intent login = new Intent(OrderPayActivity.this, Login.class);
			startActivity(login);

		}
	}

	public void showAutoLoginOnPost(HashMap<String, AbsModule> result) {
		if (dataLoadDialog.isShowing()) {
			dataLoadDialog.dismiss();
		}
		if (result.containsKey("0")) {
			info = getLoginInfo();
		
			float mon = Float.valueOf(info.getMoney()) / 100;
			DecimalFormat df = new DecimalFormat("#0.00");
			String facevalue = df.format(mon);
			current = mon;
			System.out.println(current);
			if ("0.00".equals(facevalue)) {
				tv_pay.setText(0+"元");
			}else {
				tv_pay.setText(facevalue + "元");
			}
			
			
		} else {
			toast(loginModule.message);
			SharePrefenceUtil su = new SharePrefenceUtil(OrderPayActivity.this,
					SharePrefenceUtil.USERINFO);
			su.clear();
			MyLotterActivity.loginInfo = null;
			IConstants.localUserMoney = 0;
			Intent intent = new Intent(OrderPayActivity.this, Login.class);
			startActivity(intent);
		}

	}

	public void onBackClick(View view) {
		finish();
	}

	public void onPayClick(View view) {
		
		startThread();
		String nom = (String) tv_zhifu.getText();
		
		money =Float.valueOf(nom);
		
		
	}

	public void showFromOrderOnPost(HashMap<String, AbsModule> result) {
		if (dataLoadDialog.isShowing()) {
			dataLoadDialog.dismiss();
		}
		money=Float.valueOf((String) tv_zhifu.getText());
		String code = fromOrdermodule.code;
		if ("0000".equals(code)) {
			Intent intent = new Intent(this, PayActivity.class);
			
			IConstants.localUserMoney = current - money;
			intent.putExtra("tag", 1);
			intent.putExtra("money", current + "");
			startActivity(intent);
		} else if ("5004".equals(code)) {
			toast(fromOrdermodule.message);

			SharePrefenceUtil su = new SharePrefenceUtil(this,
					SharePrefenceUtil.USERINFO);
			su.clear();
			MyLotterActivity.loginInfo = null;
			Intent login = new Intent(OrderPayActivity.this, Login.class);
			startActivity(login);

		} else if ("1015".equals(code)) {
			toast(fromOrdermodule.message);
		} else if ("9999".equals(code)) {
			toast(fromOrdermodule.message);
		} else if ("1014".equals(code)) {
			Intent intent = new Intent(this, PayActivity.class);
//			IConstants.localUserMoney = current - money;
			intent.putExtra("tag", 2);
			intent.putExtra("money", current + "");
			startActivity(intent);
		}
	

	}
	@Override
	public void showOnPost(HashMap<String, AbsModule> result) {
		if (dataLoadDialog.isShowing()) {
			dataLoadDialog.dismiss();
		}
		
		//红包解析数据
		if(0 == isPastRedpacket){
			if (module2.list1 != null) {
			adapter = new MyAdapter(module2.list1);
			listView.setAdapter(adapter);
			}
			return;
		}
	
		String code = module.code;
		if ("0000".equals(code)) {
			Intent intent = new Intent(this, PayActivity.class);
			IConstants.localUserMoney = current - money;
			intent.putExtra("tag", 1);
			intent.putExtra("money", current + "");
			startActivity(intent);
			IConstants.list_selectcode.clear();
			IConstants.list_sevencolor.clear();
			IConstants.list_three.clear();
			IConstants.list_five.clear();
		} else if ("5004".equals(code)) {
			toast(module.message);

			SharePrefenceUtil su = new SharePrefenceUtil(this,
					SharePrefenceUtil.USERINFO);
			su.clear();
			MyLotterActivity.loginInfo = null;
			Intent login = new Intent(OrderPayActivity.this, Login.class);
			startActivity(login);

		} else if ("1015".equals(code)) {
			toast(module.message);
		} else if ("9999".equals(code)) {
			toast(fromOrdermodule.message);
		} else if ("1014".equals(code)) {
			Intent intent = new Intent(this, PayActivity.class);
			intent.putExtra("tag", 2);
			intent.putExtra("money", current + "");
			startActivity(intent);
		}
		
		
		
	}
	@Override
	protected void onResume() {
		super.onResume();
		if(MyApplication.isOrderPay){
			MyApplication.isOrderPay = false;
			finish();
		}
		
	}

	@Override
	public void startThread() {
		if (!(dataLoadDialog.isShowing())) {
			dataLoadDialog.show();
			dataLoadDialog.setContentView(R.layout.progressbar_caipiao);
		}
		isPastRedpacket = 1;
		info = getLoginInfo();
		
		if (null != info) {

			if (!(TextUtils.isEmpty(issure)) && !(TextUtils.isEmpty(pid))) {
				String md5 = info.getId()+info.getLogintoken()+IConstants.key;
				String signcode = Md5Utils.md5(md5);
				fromOrderaction = new TradFromOrderAction(info.getId(),
						info.getLogintoken(), pid,signcode);
				fromOrdermodule = new TradFromOrderModule();
				fromOrderpresistence = new TradFromOrderPresistence(this);
				fromOrderpresistence.setActitons(fromOrderaction);
				fromOrderpresistence.setModule(fromOrdermodule);
				fromOrderpresistence.execute();
			} else {
				String md5 = info.getId()+info.getLogintoken()+IConstants.key;
				String signcode = Md5Utils.md5(md5);
				action = new TradAction(info.getId(), info.getLogintoken(),
						lotteryid, issueid, beishu, betting,id,signcode);
				module = new TradModule();
				presistence = new TradPresistence(this);
				presistence.setActitons(action);
				presistence.setModule(module);
				presistence.execute();
			}

		} else {

			Toast.makeText(this, "请先登录", Toast.LENGTH_SHORT).show();
			Intent login = new Intent(OrderPayActivity.this, Login.class);
			startActivity(login);

		}

	}
	public void startThread2() {
		isPastRedpacket = 0;
		if (dataLoadDialog.isShowing()) {
			dataLoadDialog.dismiss();
		}
		String md5 = MyLotterActivity.loginInfo.getId()+MyLotterActivity.loginInfo.getLogintoken()+IConstants.key;
		String signcode = Md5Utils.md5(md5);
		action2 = new HongbaoAction(MyLotterActivity.loginInfo.getId(),
				MyLotterActivity.loginInfo.getLogintoken(),signcode);
		module2 = new HongbaoModule();
		module2.flag = 0;
		action2.flag = 0;
		
		presistence2 = new HongbaoPresistence(this);
		presistence2.setActitons(action2);
		presistence2.setModule(module2);
		presistence2.execute();

	}

}
